package com.bringway.p2p.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bringway.framework.controller.JsonController;
import com.bringway.framework.util.RequestUtils;
import com.bringway.p2p.service.ImageService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/image")
public class ImageController extends JsonController {
	
	@Autowired
	private ImageService imageService;
	
	/**
	 * @作者 lk  
	 * @方法名 insertImage  
	 * @功能 TODO 新增上传图片
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/insertImage")
	public ModelAndView insertImage(HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String title = RequestUtils.getStringParameter(request, "title");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String ul = RequestUtils.getStringParameter(request, "ul");
		String attid = RequestUtils.getStringParameter(request, "attid");
		String paixu = RequestUtils.getStringParameter(request, "paixu");
		paramMap.put("title", title);
		paramMap.put("userid", userid);
		paramMap.put("ul", ul);
		paramMap.put("attid", attid);
		paramMap.put("paixu", paixu);
		
		boolean imag = imageService.insertImage(paramMap);
		if (imag) {
			result.put("success", true);
			result.put("add", imag);
		} else {
			result.put("success", true);
			result.put("msg", "新增失败!");
		}
		serialize(request, response, result);
		return null;
		
	}
	
	/**
	 * @作者 lk  
	 * @方法名 queryImagesList  
	 * @功能 TODO 获取上传图片信息列表
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/queryImagesList")
	public ModelAndView queryImagesList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Integer start = RequestUtils.getIntParameter(request, "start");
		Integer limit = RequestUtils.getIntParameter(request, "limit");
		Integer page = RequestUtils.getIntParameter(request, "page");//新版本,传参
		String startDate = RequestUtils.getStringParameter(request, "startDate");
		String endDate = RequestUtils.getStringParameter(request, "endDate");
		String title = RequestUtils.getStringParameter(request, "title");
		String imgState = RequestUtils.getStringParameter(request, "imgState");
		if (null!=page) {//新版本,传参
			start=(page-1)*limit;
		}
		JSONObject params = new JSONObject();
		JSONObject result = new JSONObject();
		params.put("start", start);
		params.put("limit", limit);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		params.put("title", title);
		params.put("imgState", imgState);
		List<Map<String, Object>> list = imageService.queryImagesList(params);
		int count = imageService.getImagesCount(params);
		result.put("data", list);
		result.put("total", count);
		if (null!=page) {//新版本,传参
			int totalPage=count%limit==0?count/limit:count/limit+1;
			result.put("totalPage", totalPage);
		}
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/updateImages")
	public ModelAndView updateImages(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String id = RequestUtils.getStringParameter(request, "id");
		String title = RequestUtils.getStringParameter(request, "title");
//		String showsj = RequestUtils.getStringParameter(request, "showsj");
		String userid = RequestUtils.getStringParameter(request, "userid");
		String ul = RequestUtils.getStringParameter(request, "ul");
		String attid = RequestUtils.getStringParameter(request, "attid");
		String paixu = RequestUtils.getStringParameter(request, "paixu");
		paramMap.put("id", id);
		paramMap.put("title", title);
//		paramMap.put("showsj", showsj);
		paramMap.put("userid", userid);
		paramMap.put("ul", ul);
		paramMap.put("attid", attid);
		paramMap.put("paixu", paixu);
		boolean imag = imageService.updateImages(paramMap);
		if (imag) {
			result.put("imag", true);
		}else {
			result.put("image", false);
			result.put("image", "更新失败");
		}
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/publicImage")
	public ModelAndView publicImage(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String id = RequestUtils.getStringParameter(request, "id");
		String state = RequestUtils.getStringParameter(request, "state");
		String orderNum = RequestUtils.getStringParameter(request, "paixu");
		paramMap.put("id", id);
		paramMap.put("state", state);
		paramMap.put("orderNum", orderNum);
		boolean imag = imageService.publicImage(paramMap);
		result.put("imag", imag);
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}
	@RequestMapping("/huiShouImage")
	public ModelAndView huiShouImage(HttpServletRequest request,HttpServletResponse response) throws Exception{
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap = new HashMap<>();
		String id = RequestUtils.getStringParameter(request, "id");
		String state = RequestUtils.getStringParameter(request, "state");
		paramMap.put("id", id);
		paramMap.put("state", state);
		boolean imag = imageService.HuiShouImage(paramMap);
		result.put("imag", imag);
		result.put("success", true);
		serialize(request, response, result);
		return null;
	}
}
